package com.hou.jvm;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.math.BigInteger;

//利用代理无限生成类,造成方法区内存溢出
// -XX:MaxMetaspaceSize=1m  默认是无限的,元空间最大内存
public class MethodAreaTest {
    static class OOMObject{}
    public static void main(String[] args) {
        while(true){
            Enhancer enhancer = new Enhancer();//工具类
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                    System.out.println("---前置代理---");
                    return methodProxy.invokeSuper(obj,args);
                }
            });
            enhancer.create();//创建子类
        }
    }

}
